package fileIO;

import java.util.Calendar;
import java.util.Formatter;

public class FormatterDemo {
	public static void main(String[] args) {
//		test();
//		test1();
//		test2();
//		timeDateFormat();
//		test3();
//		fieldWidthDemo();
//		precisionDemo();
//		leftJustify();
		test5();
	}

	private static void test5() {
		Formatter fmt = new Formatter();
		fmt.format("%(d", -100);
		System.out.println(fmt);
		fmt = new Formatter();
		fmt.format("%#x", 20);
		System.out.println(fmt);
	}

	private static void leftJustify() {
		Formatter fmt = new Formatter();
		fmt.format("|%10.2f|", 123.123);
		System.out.println(fmt);
		fmt = new Formatter();
		fmt.format("|%-10.2f|", 123.123);
		System.out.println(fmt);
	}

	private static void precisionDemo() {
		Formatter fmt = new Formatter();
		fmt.format("%.4f", 123.1234578);
		System.out.println(fmt);
		fmt = new Formatter();
		fmt.format("%16.2e", 123.1234578);
		System.out.println(fmt);
		fmt = new Formatter();
		fmt.format("%.15s", "formatting with java is now easy.");
		System.out.println(fmt);
	}

	private static void fieldWidthDemo() {
		Formatter fmt;
		for (int i = 0; i < 10; i++) {
			fmt = new Formatter();
			fmt.format("%4d %4d %4d s", i,i*i,i*i*i);
			System.out.println(fmt);
		}
	}

	private static void test3() {
		Formatter fmt = new Formatter();
		fmt.format("|%f|%n|%12f|%n|%012f|", 10.12345,10.12345,10.12345);
		System.out.println(fmt);
	}

	private static void timeDateFormat() {
		Formatter fmt = new Formatter();
		Calendar cal = Calendar.getInstance();
		fmt.format("%tr", cal);
		System.out.println(fmt);
		fmt = new Formatter();
		fmt.format("%tc", cal);
		System.out.println(fmt);
		fmt = new Formatter();
		fmt.format("%tl:%tM", cal,cal);
		System.out.println(fmt);
		fmt = new Formatter();
		fmt.format("%tB %tb %tm", cal,cal,cal);
		System.out.println(fmt);
	}

	private static void test2() {
		Formatter fmt = new Formatter();
		fmt.format("%a", 123.123);
		System.out.println(fmt);
	}

	private static void test1() {
		Formatter fmt = new Formatter();
		for(double i=1000;i<1.0e+10;i*=100){
			fmt.format("%g", i);
			System.out.println(fmt);
		}
		fmt.format("%s", "abc");
		System.out.println(fmt);
	}

	private static void test() {
		Formatter fmt = new Formatter();
		fmt.format("formatting %s is easy %d %f", "with Java",10,98.6);
		System.out.println(fmt);
	}
}
